package org.yxj.lib_storage;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;


import java.io.File;
import java.io.IOException;

/**
 * @author on 2023/10/30
 * <p>
 * 存储相关
 */
public class SDCardUtil {

    private String ROOT_FILE_NAME = "";

    public Context context;


    public SDCardUtil(Context context, String ROOT_FILE_NAME) {
        this.context = context;
        this.ROOT_FILE_NAME = ROOT_FILE_NAME;
    }


    /**
     * 获取根目录名字
     *
     * @return
     */
    public String getROOT_FILE_NAME() {
        return ROOT_FILE_NAME;
    }


    /**
     * 获取文件存储
     */
    public String getRootPath() {
        File file = new File(getRoot() + File.separator + ROOT_FILE_NAME);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath();
    }



    /**
     * 获取文件存储
     */
    public String getRoot() {
        String rootPath = "";
        if (isHaveSDCard()) {
            if (isAfter29()) {
                rootPath = after29StoragePath(context);
            } else {
                rootPath = before29StoragePath(context);
            }
        } else {
            rootPath = noSdcardStoragePath(context);
        }

        File file = new File(rootPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        return file.getAbsolutePath();
    }



    /**
     * sdk 29 版本以前文件存储路径
     *
     * @return
     */
    private String before29StoragePath(Context context) {
        String fileDir;
        // sd卡的根目錄
        fileDir = Environment.getExternalStorageDirectory().getPath();
        // sd卡根目录不存在，使用缓存目录
        if (TextUtils.isEmpty(fileDir)) {
            File f = context.getExternalCacheDir();
            fileDir = f.getPath();
        }
        LogUtils.LOG_V("[SDCardUtil]->Android10以下 = " + fileDir);
        return fileDir;
    }



    /**
     * Context APi 中的获取文件目录
     *
     * sdk 29 版本以上文件存储路径 包含29
     *
     * @return
     */
    private String after29StoragePath(Context context) {
        File file = context.getExternalFilesDir(null);
        String fileDir = file.getAbsolutePath();
        if (TextUtils.isEmpty(fileDir)) {
            File f = context.getExternalCacheDir();
            fileDir = f.getPath();
        }
        LogUtils.LOG_V("[SDCardUtil]->Android10以上 = " + fileDir);
        return fileDir;
    }


    /**
     * 无内存卡 ，使用内存目錄
     *
     * @param context
     * @return
     */
    private String noSdcardStoragePath(Context context) {
        File f = context.getFilesDir();
//        File f = context.getCacheDir();
        String fileDir = f.getPath();
        LogUtils.LOG_V("[SDCardUtil]-> 无内存卡 = " + fileDir);
        return fileDir;
    }


    /**
     * @return
     */
    public String getImgPath() {
        String path = getRootPath() + File.separator + "sharePicture";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
        File imgFile = new File(path, "share_" + System.currentTimeMillis() + ".png");
        try {
            imgFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imgFile.getPath();
    }



    /**
     * 是否有Sdcard
     *
     * @return
     */
    public static boolean isHaveSDCard() {
        boolean isSdCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        return isSdCard;
    }


    /**
     * 是否是29版本以上 包含29
     *
     * @return
     */
    public static boolean isAfter29() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
    }
}
